<template>
  <el-card>
    <el-table border style="margin: 10px 0px" :data="skuArr">
      <el-table-column
        label="序号"
        width="100px"
        type="index"
        align="center"
      ></el-table-column>
      <el-table-column
        label="名称"
        width="175px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        width="175px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="默认图片" width="175px">
        <template #="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 150px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="重量(g)"
        width="175px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格(元)"
        width="175px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" width="250px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            v-if="row.isSale == 1"
            @click="downSale(row)"
            type="info"
            size="small"
            icon="Bottom"
            title="添加SKU"
          ></el-button>
          <el-button
            v-else
            type="success"
            @click="upSale(row)"
            size="small"
            icon="Top"
            title="添加SKU"
          ></el-button>
          <el-button
            @click="updateSku"
            type="primary"
            size="small"
            icon="Edit"
            title="更新SKU"
          ></el-button>
          <el-button
            @click="findSku(row)"
            type="info"
            size="small"
            icon="InfoFilled"
            title="查看SKU列表"
          ></el-button>
          <el-popconfirm
            :title="`你确定要删除${row.skuName}`"
            width="200px"
            @confirm="deleteSku(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                icon="Delete"
                title="删除SPU"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器组件 -->
    <el-pagination
      @current-change="handler"
      @size-change="handler1"
      v-model:current-page="pageNo"
      v-model:page-size="limit"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      :small="false"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
    />
    <!-- 抽屉组件:用户展示商品的详情信息 -->
    <el-drawer v-model="drawer">
      <!-- 标题部分 -->
      <template #header>
        <h4>查看商品的详情</h4>
      </template>
      <!-- 内容部分 -->
      <template #default>
        <el-row style="margin: 15px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 15px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 15px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 15px 0px">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="(item, index) in skuInfo.skuAttrValueList"
              :key="item.id"
              style="margin: 5px"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 15px 0px">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="(item, index) in skuInfo.skuSaleAttrValueList"
              :key="item.id"
              style="margin: 5px"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 15px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="150px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img :src="item.imgUrl" style="width: 100%" alt="" />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
// 引入请求API
import {
  reqSkuList,
  reqSaleSku,
  reqCancelSale,
  reqSkuInfo,
  reqRemoveSku,
} from '../../../api/product/sku'
// 引入ts类型
import type {
  SkuResponseData,
  SkuData,
  SkuInfoData,
} from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'
// 分页器默认页码
let pageNo = ref<number>(1)
// 一页展示多少个数据
let limit = ref<number>(10)
// 数据总数
let total = ref<number>(0)
// 用来存储sku数据的数组
let skuArr = ref<SkuData[]>([])
// 控制抽屉组件的显示与隐藏
let drawer = ref<boolean>(false)
// sku信息
let skuInfo = ref<SkuData>({})

// 在组件刚刚挂载的时候就要获取一个数据
onMounted(() => {
  getHasSku()
})
// 封装一个获取已有的SKU数据的方法
const getHasSku = async (page = 1) => {
  // 设置默认页码
  pageNo.value = page
  let result: SkuResponseData = await reqSkuList(pageNo.value, limit.value)
  if (result.code == 200) {
    // 存储数据
    total.value = result.data.total
    skuArr.value = result.data.records
  }
}
// 当页码变化的时候触发
const handler = (page: number) => {
  // 更改当前页面，重新发请求获取数据
  pageNo.value = page
  getHasSku(pageNo.value)
}
// 分页器下拉菜单发生变化的时候触发
const handler1 = (pageSize: number) => {
  // 更改当前页面一页的数据量,重新发请求获取数据,默认回到第一页
  limit.value = pageSize
  getHasSku()
}
// 商品下架的回调
const downSale = async (row: SkuData) => {
  // API请求
  const result = await reqCancelSale(row.id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '商品下架成功',
    })
    // 重新发请求获取数据
    getHasSku(pageNo.value)
  } else {
    ElMessage({
      type: 'error',
      message: '商品下架失败',
    })
  }
}
// 商品上架的回调
const upSale = async (row: SkuData) => {
  // API请求
  const result = await reqSaleSku(row.id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '商品上架成功',
    })
    // 重新发请求获取数据
    getHasSku(pageNo.value)
  } else {
    ElMessage({
      type: 'error',
      message: '商品上架失败',
    })
  }
}
// 更新SKU的回调
const updateSku = () => {
  ElMessage({
    type: 'success',
    message: '程序员在努力的开发中......',
  })
}
// 打开抽屉组件的回调
const findSku = async (row: SkuData) => {
  // 显示抽屉组件
  drawer.value = true
  // 发请求获取SKU数据
  const result: SkuInfoData = await reqSkuInfo(row.id)
  // 存储SKU信息
  skuInfo.value = result.data
}
// 删除SKU商品的回调
const deleteSku = async (id: number | string) => {
  const result: any = await reqRemoveSku(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    // 再次获取SKU数据
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '系统数据不能删除',
    })
  }
}
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
